"use client"

import { useState } from "react"
import { Card } from "@/components/ui/card"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogFooter,
} from "@/components/ui/dialog"
import { Badge } from "@/components/ui/badge"
import { Eye, Truck } from 'lucide-react'

const initialOrders = [
  {
    id: "ORD001",
    customer: "张三",
    address: "上海市浦东新区陆家嘴街道123号",
    products: [
      { name: "经典白色T恤", quantity: 2, price: 99 },
      { name: "黑色基础款T恤", quantity: 1, price: 99 },
    ],
    total: 297,
    status: "待发货",
    date: "2024-01-28",
  },
  {
    id: "ORD002",
    customer: "李四",
    address: "北京市朝阳区建国门外大街456号",
    products: [
      { name: "灰色连帽卫衣", quantity: 1, price: 199 },
    ],
    total: 199,
    status: "已发货",
    date: "2024-01-27",
  },
  {
    id: "ORD003",
    customer: "王五",
    address: "广州市天河区珠江新城789号",
    products: [
      { name: "经典白色T恤", quantity: 3, price: 99 },
    ],
    total: 297,
    status: "已完成",
    date: "2024-01-26",
  },
]

export default function OrdersPage() {
  const [orders, setOrders] = useState(initialOrders)
  const [selectedOrder, setSelectedOrder] = useState(null)
  const [isDetailsOpen, setIsDetailsOpen] = useState(false)
  const [isShippingDialogOpen, setIsShippingDialogOpen] = useState(false)
  const [orderToShip, setOrderToShip] = useState(null)

  const handleViewDetails = (order) => {
    setSelectedOrder(order)
    setIsDetailsOpen(true)
  }

  const handleShipOrder = (order) => {
    setOrderToShip(order)
    setIsShippingDialogOpen(true)
  }

  const confirmShipOrder = () => {
    setOrders(orders.map(order => 
      order.id === orderToShip.id ? { ...order, status: "已发货" } : order
    ))
    setIsShippingDialogOpen(false)
  }

  const getStatusBadge = (status) => {
    switch (status) {
      case "待发货":
        return <Badge variant="outline" className="bg-yellow-50 text-yellow-600 border-yellow-300">待发货</Badge>
      case "已发货":
        return <Badge variant="outline" className="bg-blue-50 text-blue-600 border-blue-300">已发货</Badge>
      case "已完成":
        return <Badge variant="outline" className="bg-green-50 text-green-600 border-green-300">已完成</Badge>
      default:
        return <Badge variant="outline">{status}</Badge>
    }
  }

  return (
    <div className="p-4 sm:p-6 lg:p-8 space-y-4 sm:space-y-6">
      <div className="flex justify-between items-center">
        <h1 className="text-2xl font-bold">订单管理</h1>
      </div>

      <Card>
        <div className="overflow-x-auto">
          <Table>
            <TableHeader>
              <TableRow>
                <TableHead>订单编号</TableHead>
                <TableHead>客户名称</TableHead>
                <TableHead className="hidden sm:table-cell">收货地址</TableHead>
                <TableHead>订单金额</TableHead>
                <TableHead className="hidden sm:table-cell">订单状态</TableHead>
                <TableHead className="hidden sm:table-cell">下单时间</TableHead>
                <TableHead className="text-right">操作</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {orders.map((order) => (
                <TableRow key={order.id}>
                  <TableCell className="font-medium">{order.id}</TableCell>
                  <TableCell>{order.customer}</TableCell>
                  <TableCell className="hidden sm:table-cell">{order.address}</TableCell>
                  <TableCell>¥{order.total}</TableCell>
                  <TableCell className="hidden sm:table-cell">{getStatusBadge(order.status)}</TableCell>
                  <TableCell className="hidden sm:table-cell">{order.date}</TableCell>
                  <TableCell className="text-right">
                    <div className="flex justify-end gap-2">
                      <Button
                        variant="outline"
                        size="sm"
                        onClick={() => handleViewDetails(order)}
                      >
                        <Eye className="h-4 w-4 sm:mr-2" />
                        <span className="hidden sm:inline">查看详情</span>
                      </Button>
                      {order.status === "待发货" && (
                        <Button
                          variant="outline"
                          size="sm"
                          className="text-amber-500 hover:text-amber-600"
                          onClick={() => handleShipOrder(order)}
                        >
                          <Truck className="h-4 w-4 sm:mr-2" />
                          <span className="hidden sm:inline">发货</span>
                        </Button>
                      )}
                    </div>
                  </TableCell>
                </TableRow>
              ))}
            </TableBody>
          </Table>
        </div>
      </Card>

      <Dialog open={isDetailsOpen} onOpenChange={setIsDetailsOpen}>
        <DialogContent className="sm:max-w-[600px] max-h-[90vh] overflow-y-auto">
          <DialogHeader>
            <DialogTitle>订单详情</DialogTitle>
            <DialogDescription>
              订单编号: {selectedOrder?.id}
            </DialogDescription>
          </DialogHeader>
          {selectedOrder && (
            <div className="space-y-4">
              <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
                <div>
                  <h3 className="font-medium text-gray-500">客户信息</h3>
                  <p className="mt-1">{selectedOrder.customer}</p>
                  <p className="mt-1 text-sm text-gray-500">{selectedOrder.address}</p>
                </div>
                <div>
                  <h3 className="font-medium text-gray-500">订单信息</h3>
                  <p className="mt-1">状态: {getStatusBadge(selectedOrder.status)}</p>
                  <p className="mt-1">下单时间: {selectedOrder.date}</p>
                </div>
              </div>

              <div>
                <h3 className="font-medium text-gray-500 mb-2">商品清单</h3>
                <div className="overflow-x-auto">
                  <Table>
                    <TableHeader>
                      <TableRow>
                        <TableHead>商品名称</TableHead>
                        <TableHead>数量</TableHead>
                        <TableHead>单价</TableHead>
                        <TableHead className="text-right">小计</TableHead>
                      </TableRow>
                    </TableHeader>
                    <TableBody>
                      {selectedOrder.products.map((product, index) => (
                        <TableRow key={index}>
                          <TableCell>{product.name}</TableCell>
                          <TableCell>{product.quantity}</TableCell>
                          <TableCell>¥{product.price}</TableCell>
                          <TableCell className="text-right">
                            ¥{product.quantity * product.price}
                          </TableCell>
                        </TableRow>
                      ))}
                      <TableRow>
                        <TableCell colSpan={3} className="text-right font-medium">
                          总计
                        </TableCell>
                        <TableCell className="text-right font-bold">
                          ¥{selectedOrder.total}
                        </TableCell>
                      </TableRow>
                    </TableBody>
                  </Table>
                </div>
              </div>
            </div>
          )}
        </DialogContent>
      </Dialog>

      <Dialog open={isShippingDialogOpen} onOpenChange={setIsShippingDialogOpen}>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>确认发货</DialogTitle>
            <DialogDescription>
              您确定要为订单 {orderToShip?.id} 发货吗？
            </DialogDescription>
          </DialogHeader>
          <DialogFooter>
            <Button variant="outline" onClick={() => setIsShippingDialogOpen(false)}>取消</Button>
            <Button onClick={confirmShipOrder}>确认发货</Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </div>
  )
}

